package com.photoeditor.demo.util.store;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.photoeditor.demo.model.event.RefreshFilterCategory;
import com.photoeditor.demo.model.event.RefreshStickerEvent;
import com.photoeditor.demo.model.store.module.BaseOnlineManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import static com.photoeditor.demo.model.event.RefreshStickerEvent.OPERATION_REFRESH;


//import com.photoeditor.version.RateManager;


/**

 */
public class StoreDownloadSender {

    public final static String FILTER_START_PACKAGE_NAME = "com.photoeditor.imagefilter.plugins";

    public final static String ACTION_DOWNLOAD_FINISH = "com.photoeditor.filterstore.downloadfinish";
    public final static String ACTION_DELETE_ZIP = "com.photoeditor.filterstore.deletezip";

    public final static String EXTRA_PACKAGE_NAME = "package_name";

    public static HashMap<IDownloadListener, DownOverBrocastReceiver> mListenerMap = new HashMap<>();

    public static void registerDownOverBrocastReceiver(Activity mActivity, IDownloadListener mListener){
        try {
            DownOverBrocastReceiver receiver = mListenerMap.get(mListener);
            if(receiver == null) {
                receiver = new DownOverBrocastReceiver(mListener);
                mListenerMap.put(mListener, receiver);
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(ACTION_DOWNLOAD_FINISH);
                intentFilter.addAction(ACTION_DELETE_ZIP);
                mActivity.registerReceiver(receiver, intentFilter);
            }
        } catch (Throwable e){

        }
    }

    public static void unRegisterDownOverBrocastReceiver(Activity mActivity, IDownloadListener mListener){
        try {
            DownOverBrocastReceiver receiver = mListenerMap.remove(mListener);
            if(receiver != null) {
                mActivity.unregisterReceiver(receiver);
            }
        } catch (Throwable e){

        }
    }

    public static void sendDownOverBrocast(Context mContext, String pkgName, int type){
//        Intent intent = new Intent();
//        intent.setAction(ACTION_DOWNLOAD_FINISH);
//        intent.putExtra(EXTRA_PACKAGE_NAME, pkgName);
//        mContext.sendBroadcast(intent);
        if (type == BaseOnlineManager.STORE_FUNC_TYPE_FILTER) {
            EventBus.getDefault().post(new RefreshFilterCategory());
        } else if (type == BaseOnlineManager.STORE_FUNC_TYPE_STICKER) {
            EventBus.getDefault().post(new RefreshStickerEvent(OPERATION_REFRESH, pkgName));
        }
    }

    public static void sendDeleteZIPBrocast(Context mContext, String pkgName){
        Intent intent = new Intent();
        intent.setAction(ACTION_DELETE_ZIP);
        intent.putExtra(EXTRA_PACKAGE_NAME, pkgName);
        mContext.sendBroadcast(intent);
    }

    private static class DownOverBrocastReceiver extends BroadcastReceiver{

        IDownloadListener mListener;

        DownOverBrocastReceiver(IDownloadListener listener){
            mListener = listener;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent == null) return;
            String action = intent.getAction();
            if(TextUtils.equals(action, ACTION_DOWNLOAD_FINISH)){
                final String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
                mListener.onDownloadOver(packageName);
            } else if(TextUtils.equals(action, ACTION_DELETE_ZIP)){
                final String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
                mListener.onDeleteZIP(packageName);
            }
        }
    }

    public interface IDownloadListener{
        void onDownloadOver(String packageName);
        void onDeleteZIP(String packageName);
    }
}
